<template>
  <el-dialog
    :title="!dataForm.agentSpaceId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人id" prop="agentId">
      <el-input v-model="dataForm.agentId" placeholder="经纪人id"></el-input>
    </el-form-item>
    <el-form-item label="经纪人名称" prop="agentName">
      <el-input v-model="dataForm.agentName" placeholder="经纪人名称"></el-input>
    </el-form-item>
    <el-form-item label="楼市圈图片" prop="agentImg">
      <el-input v-model="dataForm.agentImg" placeholder="楼市圈图片"></el-input>
    </el-form-item>
    <el-form-item label="发布日期" prop="agentDate">
      <el-input v-model="dataForm.agentDate" placeholder="发布日期"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          agentId: '',
          agentName: '',
          agentImg: '',
          agentDate: '',
          agentSpaceId: 0,
        },
        dataRule: {
          agentId: [
            { required: true, message: '经纪人id不能为空', trigger: 'blur' }
          ],
          agentName: [
            { required: true, message: '经纪人名称不能为空', trigger: 'blur' }
          ],
          agentImg: [
            { required: true, message: '楼市圈图片不能为空', trigger: 'blur' }
          ],
          agentDate: [
            { required: true, message: '发布日期不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.agentSpaceId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.agentSpaceId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/spacehome/info/${this.dataForm.agentSpaceId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.agentId = data.spaceHome.agentId
                this.dataForm.agentName = data.spaceHome.agentName
                this.dataForm.agentImg = data.spaceHome.agentImg
                this.dataForm.agentDate = data.spaceHome.agentDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/spacehome/${!this.dataForm.agentSpaceId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'agentId': this.dataForm.agentId,
                'agentName': this.dataForm.agentName,
                'agentImg': this.dataForm.agentImg,
                'agentDate': this.dataForm.agentDate,
                'agentSpaceId': this.dataForm.agentSpaceId || undefined,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
